<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Construction from Specific Values Without Precision Loss</title>
<link rel="stylesheet" href="../../../../multiprecision.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Chapter 1. Boost.Multiprecision">
<link rel="up" href="../fp_eg.html" title="Examples">
<link rel="prev" href="../fp_eg.html" title="Examples">
<link rel="next" href="aos.html" title="Area of Circle">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../fp_eg.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fp_eg.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="aos.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="boost_multiprecision.tut.floats.fp_eg.floatbuiltinctor"></a><a class="link" href="floatbuiltinctor.html" title="Construction from Specific Values Without Precision Loss">Construction
          from Specific Values Without Precision Loss</a>
</h5></div></div></div>
<p>
            Construction of multiprecision types from built-in floating-point types
            can lead to potentially unexpected, yet correct, results. Consider, for
            instance constructing an instance of <code class="computeroutput"><span class="identifier">cpp_dec_float_50</span></code>
            from the literal built-in floating-point <code class="computeroutput"><span class="keyword">double</span></code>
            value 11.1.
          </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iomanip</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">limits</span><span class="special">&gt;</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_dec_float</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
  <span class="keyword">using</span> <span class="identifier">my_dec_100</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_dec_float_50</span><span class="special">;</span>

  <span class="keyword">const</span> <span class="identifier">my_dec_100</span> <span class="identifier">f11</span><span class="special">(</span><span class="number">11.1</span><span class="special">);</span>

  <span class="comment">// On a system with 64-bit double:</span>
  <span class="comment">// 11.09999999999999964472863211994990706443786621093750</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">setprecision</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">my_dec_100</span><span class="special">&gt;::</span><span class="identifier">digits10</span><span class="special">)</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">fixed</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">f11</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
            In this example, the system has a 64-bit built in <code class="computeroutput"><span class="keyword">double</span></code>
            representation. The variable <code class="computeroutput"><span class="identifier">f11</span></code>
            is initialized with the literal <code class="computeroutput"><span class="keyword">double</span></code>
            value 11.1. Recall that built-in floating-point representations are based
            on successive binary fractional approximations. These are, in fact, very
            close approximations. But they are approximations nonetheless, having
            their built-in finite precision.
          </p>
<p>
            For this reason, the full multiple precision value of the <code class="computeroutput"><span class="keyword">double</span></code> approximation of 11.1 is given
            by the large value shown above. Observations show us that the value is
            reliable up to the approximate 15 decimal digit precision of built-in
            64-bit <code class="computeroutput"><span class="keyword">double</span></code> on this system.
          </p>
<p>
            If the exact value of 11.1 is desired (within the wider precision of
            the multiprecision type), then construction from literal string or from
            a rational integral construction/division sequence should be used.
          </p>
<pre class="programlisting"><span class="keyword">const</span> <span class="identifier">my_dec_100</span> <span class="identifier">f11_str</span><span class="special">(</span><span class="string">"11.1"</span><span class="special">);</span>
<span class="keyword">const</span> <span class="identifier">my_dec_100</span> <span class="identifier">f11_n</span>  <span class="special">(</span><span class="identifier">my_dec_100</span><span class="special">(</span><span class="number">111</span><span class="special">)</span> <span class="special">/</span> <span class="number">10</span><span class="special">);</span>
</pre>
</div>
<div class="copyright-footer">Copyright © 2002-2020 John
      Maddock and Christopher Kormanyos<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../fp_eg.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../fp_eg.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="aos.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
